package com.gtw.modules.api.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.gtw.common.annotation.AuthIgnoreApi;
import com.gtw.common.annotation.LoginUserApi;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.modules.api.service.TbCommodityService;
import com.gtw.modules.api.service.TbShoppingCartServices;
import com.gtw.modules.entity.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * TbShoppingCart
 *
 * @author wuye
 * @email www.baidu.com
 * @date 2018-02-06 15:19:47
 */
@RestController
@RequestMapping("/api/shoppingcart")
public class TbShoppingCartController {

    @Autowired
    private TbShoppingCartServices tbShoppingCartServices;

    @Autowired
    private TbCommodityService tbCommodityService;

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public R save(@LoginUserApi TbUser tbUser, Integer commodityid, Integer num, String selected, int id) {
        TbShoppingCart tbShoppingCart = tbShoppingCartServices.findByCommodity(commodityid, tbUser.getUuid(), selected);
        if (tbShoppingCart != null) {
            tbShoppingCart.setCommoditynumber(num);
            tbShoppingCartServices.updateByPrimaryKey(tbShoppingCart);
            return R.ok().put("data", tbShoppingCart);
        }
        if (id != 0) {
            tbShoppingCart = tbShoppingCartServices.selectByPrimaryKey(id);
            tbShoppingCart.setCommoditynumber(num);
            tbShoppingCartServices.updateByPrimaryKey(tbShoppingCart);
        } else {
            tbShoppingCart = new TbShoppingCart();
            TbCommodity tbCommodity = tbCommodityService.queryObject(commodityid);
            tbShoppingCart.setUid(tbUser.getUuid());
            tbShoppingCart.setCommoditynumber(num);
            tbShoppingCart.setSelected(selected);
            tbShoppingCart.setStatus(false);
            tbShoppingCart.setShoppingtime(new Date());
            tbShoppingCart.setCommodityid(commodityid);
            tbShoppingCart.setCommoditypic(tbCommodity.getPicurl());
            tbShoppingCart.setCommodityname(tbCommodity.getCommodityname());
            tbShoppingCart.setFreight(tbCommodity.getFreight());
            tbShoppingCart.setPrice(tbCommodity.getPrice());
            tbShoppingCart.setPricediscounts(tbCommodity.getPricediscounts());
            tbShoppingCartServices.insert(tbShoppingCart);
        }

        return R.ok().put("data", tbShoppingCart);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params, @LoginUserApi TbUser tbUser) {
        params.put("uid", tbUser.getUuid());
        //查询列表数据
        Query query = new Query(params);
        Page<TbShoppingCart> pageUtil = new Page<TbShoppingCart>(query.getPage(), query.getLimit());
        Page<TbShoppingCart> page = tbShoppingCartServices.queryPageList(pageUtil, query);
        return R.ok().put("data", page);
    }


    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public R delete(@RequestBody Integer[] ids) {
        int[] success = new int[ids.length];
        for (int i = 0; i < ids.length; i++) {
            TbShoppingCart tbShoppingCart = tbShoppingCartServices.selectByPrimaryKey(ids[i]);
            tbShoppingCart.setStatus(true);
            tbShoppingCartServices.updateByPrimaryKey(tbShoppingCart);
            success[i] = ids[i];
        }
        return R.ok().put("data", success);
    }

}
